package com.example.statistics.common

import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import com.example.statistics.bean.DocumentBean
import com.example.statistics.ex.loadingDialog
import com.example.statistics.utils.Constants
import com.example.statistics.utils.file.DocumentLoading
import kotlinx.android.synthetic.main.activity_base_fragment.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import org.jetbrains.anko.support.v4.withArguments

class FileSelectorActivity : BaseFragmentActivity() {

    override val title: String
        get() = "所有文件"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        GlobalScope.launch(Dispatchers.Main) {
            val dialog = loadingDialog("")
            DocumentLoading.query(this@FileSelectorActivity) {
                val list = it.map { fragment(it.documentList) to it.name.orEmpty() }
                fragmentList = list
            }
            dialog.dismiss()
        }
        topBarRightText("确定") {
            val selectorList = mutableListOf<DocumentBean>()
            fragmentList.forEach {
                val documentBean = (it.first as FileFragment).getSelectorList()
                selectorList.addAll(documentBean)
            }
            val a = selectorList

        }
        view_pager.offscreenPageLimit = 1
        view_pager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
            }

            override fun onPageSelected(position: Int) {


                Log.w("fragment-->page",position.toString())
            }

            override fun onPageScrollStateChanged(state: Int) {

            }

        })
    }


    fun fragment(list: List<DocumentBean>): Fragment {
        val fragment = FileFragment()
        fragment.withArguments(Constants.DATA to list)
        return fragment
    }
}